/**
 * Live GPS Tracking Application
 * GPS Reader Thread
 *
 * Copyright 2009, Ivar Clemens
 */

package nl.ivarclemens.gpstrack;
import nl.ivarclemens.gpsdriver.Driver;

public class GPSThread {

    private WorkerThread thread = null;
    private Driver driver = null;
    private GPSMessageEvent listener = null;

    public GPSThread(Driver driver)
    {
        this.driver = driver;

        thread = new WorkerThread(this);
        thread.start();
    }

    public void setListener(GPSMessageEvent listener)
    {
        this.listener = listener;
    }

    public class WorkerThread extends Thread
    {
        GPSThread parent;

        public WorkerThread(GPSThread gpsThread)
        {
            parent = gpsThread;
        }

        public void run()
        {
            while(true) {
                if(driver != null && driver.isConnected()) {
                    String msg = driver.readMsg();

                    if(parent.listener != null)
                        parent.listener.dispatchMsg(msg);
                } else {
                    try {
                        driver.reconnect();
                        Thread.sleep(2000);
                    } catch(Exception e) {}
                }
            }
        }
    }
}
